﻿#include "KingScorpion.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Map.h"
#include "../MapObject.h"
#include "../../../Shared/Enums.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/ServerPackets.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    KingScorpion::KingScorpion(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool KingScorpion::InAttackRange()
    {
        if (getTarget()->getCurrentMap() != getCurrentMap())
        {
            return false;
        }
        if (getTarget()->getCurrentLocation() == getCurrentLocation())
        {
            return false;
        }

        int x = std::abs(getTarget()->getCurrentLocation()->X - getCurrentLocation()->X);
        int y = std::abs(getTarget()->getCurrentLocation()->Y - getCurrentLocation()->Y);

        if (x > 2 || y > 2)
        {
            return false;
        }


        return (x <= 1 && y <= 1) || (x == y || x % 2 == y % 2);
    }

    void KingScorpion::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));

        Point *target = Functions::PointMove(getCurrentLocation(), getDirection(), 2);

        bool range = false;

        if (getCurrentMap()->ValidPoint(target))
        {
            Cell *cell = getCurrentMap()->GetCell(target);
            if (!cell->Objects.empty())
            {
                for (int o = 0; o < cell->Objects.size(); o++)
                {
                    MapObject *ob = cell->Objects[o];
                    if (ob->getRace() != ObjectType::Monster && ob->getRace() != ObjectType::Player)
                    {
                        continue;
                    }
                    if (!ob->IsAttackTarget(this))
                    {
                        continue;
                    }
                    range = true;
                    break;
                }
            }
        }

        if (range || getEnvir()->Random->Next(5) == 0)
        {
            S::ObjectRangeAttack *tempVar = new S::ObjectRangeAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            Broadcast(tempVar);
            LineAttack(2, 300, DefenceType::MACAgility);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
        else
        {
            S::ObjectAttack *tempVar2 = new S::ObjectAttack();
            tempVar2->ObjectID = ObjectID;
            tempVar2->Direction = getDirection();
            tempVar2->Location = getCurrentLocation();
            Broadcast(tempVar2);
            LineAttack(2, 300, DefenceType::ACAgility);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
        }

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;
        ShockTime = 0;
    }
}
